﻿using Newtonsoft.Json;

namespace Aurora.HttpClient
{
    internal static class ObjectExtension
    {
        public static string ToQueryString(this object model)
        {
            var queryString = string.Empty;
            if (model is null)
            {
                return queryString;
            }

            var properties = model.GetType().GetProperties();

            queryString = string.Join("&", properties.Select(d => $"{d.Name}={d.GetValue(model)}"));
            return queryString;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(this object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="input"></param>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        public static TResult DeserializeObject<TResult>(string input)
        {
            return JsonConvert.DeserializeObject<TResult>(input);
        }
    }
}
